/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Huytn.sessionBean;

import Easy.table.CreditCard;
import Easy.table.OrderDetail;
import Easy.table.Orders;
import Easy.table.Users;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DR.HUY
 */
@Stateless
public class UserSessionBean implements UserSessionBeanRemote, UserSessionBeanLocal {
    @PersistenceContext(unitName = "EasyTicket-ejbPU")
    private EntityManager em;
    private Vector contents;

    public void persist(Object object) {
        em.persist(object);
    }
    public boolean insertUser(String username, String password, String name, String dob, String address, String phone, String email, String role, String status) {
         boolean check = false;
        try {
            Users user = new Users(username, password, name);
            user.setDayOfBirth(dob);
            user.setAddress(address);
            user.setPhone(phone);
            user.setEmail(email);
            user.setRole(role);
            user.setStatus(status);
            persist(user);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public Users login(String username, String password) {
        Users result = null;
        try {
            String ejbQL= "SELECT u FROM Users u WHERE u.username = :username and u.password = :pass and u.status = :status";
            Query query = em.createQuery(ejbQL);
            query.setParameter("username", username);//username la o phia sau dau ":"
            query.setParameter("pass", password);
            query.setParameter("status", "Active");
            List list = query.getResultList();
            if(list.isEmpty()){
                
            }else{
                result = (Users)list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean updateProfile(int id, String username, String name, String dob, String address, String phone, String email) {
         boolean check = false;
        try {
            Users user = em.find(Users.class, id);
            user.setName(name);
            user.setDayOfBirth(dob);
            user.setAddress(address);
            user.setPhone(phone);
            user.setEmail(email);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean updatePassword(int id, String nPass) {
        boolean check = false;
        try {
            Users user = em.find(Users.class, id);
            user.setPassword(nPass);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public Easy.table.Users[] loadUsers(String role) {
        Users[] users = null;
        try {
            String ejbQL= "SELECT u FROM Users u WHERE u.role = :role";
            Query query = em.createQuery(ejbQL);
            query.setParameter("role", role);
            List list = query.getResultList();
            users = new Users[list.size()];
            list.toArray(users);
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    public boolean disableUser(int userID, String status) {
        boolean check = false;
        try {
            Users user = em.find(Users.class, userID);
            user.setStatus(status);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public Vector getContents() {
        return contents;
    }

    public void setContents(Vector contents) {
        this.contents = contents;
    }

    public UserSessionBean() {
        this.contents = new Vector();
    }

    public void addTicket(ShoppingCartDTO dto){
        this.contents.addElement(dto);
    }

    public void removeTicket(int i) throws Exception{
        this.contents.removeElementAt(i);
    }

    public void editTicket(int index, ShoppingCartDTO dto) throws Exception{
        this.contents.set(index, dto);
    }

    public String checkCredit(String cardNumber, int total) {
        String check= "";
        try {
            Query query = em.createNamedQuery("CreditCard.findByCardNumber");
            query.setParameter("cardNumber", cardNumber);
            List list = query.getResultList();
            CreditCard result;
            if(list.isEmpty()){
                check = "wrongCard";
            }else{
                result = (CreditCard)list.get(0);
                if(result.getBalance()>=total){
                    CreditCard card = em.find(CreditCard.class, cardNumber);
                    card.setBalance(result.getBalance()-total);
                    check = "enough";
                }else
                    check = "notEnough";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }


    public boolean insertOrder(int userID, String cardNum, String total, String date, String address, String status) {
        boolean check = false;
        try {
            Orders order = new Orders();
            Users u = new Users(userID);
            order.setUsers(u);
            CreditCard c = new CreditCard(cardNum);
            order.setCreditCard(c);
            order.setTotalPrice(total);
            order.setDate(date);
            order.setAddress(address);
            order.setStatus(status);
            persist(order);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean insertOrderDetail(int orderID, int ticketID, int quantity) {
        boolean check = false;
        try {
            OrderDetail orderd = new OrderDetail(orderID, ticketID);
            orderd.setQuantity(quantity);
            persist(orderd);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public int selectOrderId() {
         String ejbQL= "SELECT o FROM Orders o";
         Query query = em.createQuery(ejbQL);
         List ii = query.getResultList();
         Orders oo = (Orders)ii.get(ii.size()-1);
         int i = oo.getOrderID();
         return i;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
